Skip to content

add additional memory access validation in seccomp.c#3

Open
mpzanoosi wants to merge 1 commit intocanonical:mainfrom
mpzanoosi:main
Open

add additional memory access validation in seccomp.c#3
mpzanoosi wants to merge 1 commit intocanonical:mainfrom
mpzanoosi:main

Conversation

@mpzanoosi
Copy link
Copy Markdown

function sc_must_read_filter_from_file is changed to be checked against memory length validation

function sc_must_read_filter_from_file is changed to be checked against memory length validation
Comment thread seccomp.c
if (prog->len > MAX_BPF_SIZE / sizeof(struct sock_filter)) {
die("seccomp filter too large");
fclose(file); // Add this line to close the file on error
return NULL; // Add this line to handle the error properly
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function is void, so this is usesless.

Comment thread seccomp.c
// When reading syscall numbers or other data, ensure they are within valid ranges. This should be done in the loop where the file is being read:
if (prog->len > MAX_BPF_SIZE / sizeof(struct sock_filter)) {
die("seccomp filter too large");
fclose(file); // Add this line to close the file on error
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function should not close the file by itself as it received it open already. This call will make an unexpected side effect.

Comment thread seccomp.c
prog->filter = (struct sock_filter *)malloc(MAX_BPF_SIZE);
// When reading syscall numbers or other data, ensure they are within valid ranges. This should be done in the loop where the file is being read:
if (prog->len > MAX_BPF_SIZE / sizeof(struct sock_filter)) {
die("seccomp filter too large");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

die() effectively closes the whole program, so it is no return. No use of calling stuff after die().

Comment thread seccomp.c
}

// Ensure any dynamically allocated memory is properly managed. For example, if you allocate memory for a buffer, make sure to free it:
// prog->filter = (struct sock_filter *)malloc(MAX_BPF_SIZE);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Done leave commented out lines in production code;
  • The cast was fine, you should leave it;
  • Add a check for too much memory requested:
if (len_bytes > MAX_BPF_SIZE)
    die(...)

Comment thread seccomp.c
// Ensure any dynamically allocated memory is properly managed. For example, if you allocate memory for a buffer, make sure to free it:
// prog->filter = (struct sock_filter *)malloc(MAX_BPF_SIZE);
prog->filter = malloc(len_bytes);
if (prog->filter == NULL) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The next check is the same, remove it.

Comment thread seccomp.c
if (ferror(file)) {
die("cannot read filter");
}
if (num_read != len_bytes) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, remove repeating check

Comment thread seccomp.c
die("short read for filter %zu != %i", num_read, len_bytes);
}

free(prog->filter); // Add this line to free the allocated memory
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct for several reasons:

  • the next functions rely on this data being available
  • freeing will make for a dangling pointer
  • why bother with all the code above to just discard data just loaded

@RajeshC03
Copy link
Copy Markdown

The function is declared as void, so returning NULL is invalid. Instead, you could set an error flag or call exit(EXIT_FAILURE) if terminating the program is appropriate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants